package com.ruoyi.project.qyxy.jlscore.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.qyxy.enterprise.domain.EnterpriseInfo;
import com.ruoyi.project.qyxy.jlscoredtl.domain.JlOrgScoreDetail;
import com.ruoyi.project.qyxy.jlscoredtl.service.IJlOrgScoreDetailService;
import com.ruoyi.project.qyxy.qua.domain.JlQuaTemp;
import com.ruoyi.project.qyxy.qua.domain.JlScoreTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.qyxy.jlscore.mapper.JlOrgScoreMapper;
import com.ruoyi.project.qyxy.jlscore.domain.JlOrgScore;
import com.ruoyi.project.qyxy.jlscore.service.IJlOrgScoreService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 监理企业得分Service业务层处理
 * 
 * @author lcl
 * @date 2021-03-14
 */
@Service
public class JlOrgScoreServiceImpl implements IJlOrgScoreService 
{
    @Autowired
    private JlOrgScoreMapper jlOrgScoreMapper;

    @Autowired
    private IJlOrgScoreService jlOrgScoreService;

    @Autowired
    private IJlOrgScoreDetailService jlOrgScoreDetailService;

    /**
     * 查询监理企业得分
     * 
     * @param id 监理企业得分ID
     * @return 监理企业得分
     */
    @Override
    public JlOrgScore selectJlOrgScoreById(Long id)
    {
        return jlOrgScoreMapper.selectJlOrgScoreById(id);
    }

    /**
     * 查询监理企业得分列表
     * 
     * @param jlOrgScore 监理企业得分
     * @return 监理企业得分
     */
    @Override
    public List<JlOrgScore> selectJlOrgScoreList(JlOrgScore jlOrgScore)
    {
        return jlOrgScoreMapper.selectJlOrgScoreList(jlOrgScore);
    }

    /**
     * 新增监理企业得分
     * 
     * @param jlOrgScore 监理企业得分
     * @return 结果
     */
    @Override
    public int insertJlOrgScore(JlOrgScore jlOrgScore)
    {
        jlOrgScore.setCreateTime(DateUtils.getNowDate());
        return jlOrgScoreMapper.insertJlOrgScore(jlOrgScore);
    }

    /**
     * 修改监理企业得分
     * 
     * @param jlOrgScore 监理企业得分
     * @return 结果
     */
    @Override
    public int updateJlOrgScore(JlOrgScore jlOrgScore)
    {
        jlOrgScore.setUpdateTime(DateUtils.getNowDate());
        return jlOrgScoreMapper.updateJlOrgScore(jlOrgScore);
    }

    /**
     * 批量删除监理企业得分
     * 
     * @param ids 需要删除的监理企业得分ID
     * @return 结果
     */
    @Override
    public int deleteJlOrgScoreByIds(Long[] ids)
    {
        return jlOrgScoreMapper.deleteJlOrgScoreByIds(ids);
    }

    /**
     * 删除监理企业得分信息
     * 
     * @param id 监理企业得分ID
     * @return 结果
     */
    @Override
    public int deleteJlOrgScoreById(Long id)
    {
        return jlOrgScoreMapper.deleteJlOrgScoreById(id);
    }

    @Transactional(rollbackFor = {RuntimeException.class})
    @Override
    public void insertDetail(EnterpriseInfo enterprise, JlScoreTemp jlScoreTemp, List<JlQuaTemp> quaTempList) {
        JlOrgScore jlOrgScore = new JlOrgScore();
        jlOrgScore.setOrgCode(enterprise.getOrgCode());
        jlOrgScore.setOrgName(enterprise.getOrgName());
        jlOrgScore.setScore(jlScoreTemp.getScore());
        jlOrgScoreService.insertJlOrgScore(jlOrgScore);
        for (JlQuaTemp jlQuaTemp : quaTempList) {
            JlOrgScoreDetail jlOrgScoreDetail = new JlOrgScoreDetail();
            jlOrgScoreDetail.setJlId(jlOrgScore.getId());
            jlOrgScoreDetail.setQuaName(jlQuaTemp.getQuaInfo().getQuaName());
            jlOrgScoreDetail.setQuaType(jlQuaTemp.getQuaInfo().getQuaType());
            jlOrgScoreDetail.setQuaRank(jlQuaTemp.getQuaInfo().getQuaRank());
            jlOrgScoreDetail.setShortOfCount(Long.parseLong(jlQuaTemp.getShortOfCount() + ""));
            jlOrgScoreDetail.setUseJg(JSONObject.toJSONString(jlQuaTemp.getJgPersonList()));
            jlOrgScoreDetail.setUseJl(JSONObject.toJSONString(jlQuaTemp.getJlPersonList()));
            jlOrgScoreDetail.setUseJz(JSONObject.toJSONString(jlQuaTemp.getJzPersonList()));
            jlOrgScoreDetail.setUseJzh(JSONObject.toJSONString(jlQuaTemp.getJzhPersonList()));
            jlOrgScoreDetail.setUseZj(JSONObject.toJSONString(jlQuaTemp.getZjPersonList()));
            jlOrgScoreDetail.setReduceScore(jlQuaTemp.getReduceScore());
            jlOrgScoreDetail.setIsConformTo(jlQuaTemp.getQualified() ? "是":"否");
            jlOrgScoreDetailService.insertJlOrgScoreDetail(jlOrgScoreDetail);
        }
    }
}
